package com.ysxsoft.wdg.ui.activity.my.jf;

import android.content.Context;
import android.content.Intent;
import android.support.v4.widget.NestedScrollView;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

import com.ysxsoft.wdg.R;
import com.ysxsoft.wdg.base.BaseActivity;
import com.ysxsoft.wdg.bean.BaseBean;
import com.ysxsoft.wdg.bean.MyJfBean;
import com.ysxsoft.wdg.bean.response.DefaultAddressBean;
import com.ysxsoft.wdg.bean.response.MyAddressBean;
import com.ysxsoft.wdg.net.CallbackCode;
import com.ysxsoft.wdg.net.UrlHost;
import com.ysxsoft.wdg.ui.activity.address.AddressListActivity;
import com.ysxsoft.wdg.util.JsonUtils;
import com.ysxsoft.wdg.util.ToastUtils;
import com.ysxsoft.wdg.util.ViewUtils;
import com.ysxsoft.wdg.util.WebViewUtils;
import com.ysxsoft.wdg.util.sp.SharePrefUtils;
import com.ysxsoft.wdg.util.statusbar.StatusBarUtil;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import okhttp3.Call;

public class JfGoodsDetailsActivity extends BaseActivity {


    public static void start(Context context,String detail_id){
        Intent intent = new Intent(context,JfGoodsDetailsActivity.class);
        intent.putExtra("detail_id",detail_id);
        context.startActivity(intent);
    }

    @BindView(R.id.banner)
    ImageView banner;
    @BindView(R.id.tv_jf)
    TextView tvJf;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.webView)
    WebView webView;
    @BindView(R.id.scrollView)
    NestedScrollView scrollView;
    @BindView(R.id.topView)
    View topView;
    @BindView(R.id.btn_finish)
    ImageView btnFinish;
    @BindView(R.id.btn_share)
    ImageView btnShare;
    @BindView(R.id.btn_cart)
    ImageView btnCart;

    private String detail_id ="";

    @Override
    protected int getLayoutId() {
        return R.layout.activity_jf_goods_details;
    }

    @Override
    protected void initData() {
        StatusBarUtil.setStatusBarTextColor(this,true);
        initStatusBar(topView,false);
        btnCart.setVisibility(View.GONE);
        btnShare.setVisibility(View.GONE);

        WebViewUtils.init(webView);

        detail_id = getIntent().getStringExtra("detail_id");


        mApiHelper.getJfGoodsDetail(detail_id, new Observer<BaseBean<MyJfBean.ShopBean>>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(BaseBean<MyJfBean.ShopBean> baseBean) {
                if (baseBean.getCode().equals(CallbackCode.SUCCESS)){
                    MyJfBean.ShopBean shopBean = baseBean.getData();

                    ViewUtils.loadImage(mContext,shopBean.getPic(),banner);

                    tvName.setText(shopBean.getName());
                    tvJf.setText(shopBean.getScore()+"积分");

                    WebViewUtils.setH5Data(webView,shopBean.getContent());
                }
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    protected void setListener() {

    }



    @OnClick({R.id.tv_buy, R.id.btn_finish})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_buy:
                getAddressStatus();
                break;
            case R.id.btn_finish:
                finish();
                break;
        }
    }

    /**
     * 获取默认地址
     */
    private void getAddressStatus() {
        OkHttpUtils.post()
                .url(UrlHost.FEN_GET_ADDRESS)
                .addParams("uid", SharePrefUtils.getUserId())
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                if (!call.isCanceled()) {
                }
            }

            @Override
            public void onResponse(String response, int id) {
                if (response != null) {
                    DefaultAddressBean addressBean = JsonUtils.parseByGson(response, DefaultAddressBean.class);
                    if (addressBean == null) {
                        return;
                    }
                    if (CallbackCode.SUCCESS.equals(addressBean.getCode())) {
                        MyAddressBean myAddressBean = addressBean.getData();
                        Intent intent =  new Intent(JfGoodsDetailsActivity.this,JfExchangeActivity.class);
                        intent.putExtra("productid",detail_id);
                        intent.putExtra("address",myAddressBean);
                        startActivity(intent);
                        finish();
                    }else{
                        startActivity(new Intent(JfGoodsDetailsActivity.this, AddressListActivity.class));
                        ToastUtils.showToast("请添加默认地址");
                    }
                } else {
                }
            }
        });
    }
}
